import { Router, NavigationEnd } from '@angular/router';
import { AppUtils } from './api/AppUtils';
import { Component, OnInit } from '@angular/core';
import { Platform, NavController, ToastController } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { ApiUrl } from './api/ApiUrl';
import { AppMinimize } from '@ionic-native/app-minimize/ngx';
/** @Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['menustyle.scss']

})
export class AppComponent implements OnInit {
  user: object;
  public appPages = [
    {
      title: '银企对接情况',
      url: '/connect',
      icon: 'people'
    },
    {
      title: '金融惠企大走访',
      url: '/home',
      icon: 'walk'
    },*/
/** {
  title: '服务银行',
  url: '/home',
  icon: 'home'
},
{
  title: '服务企业',
  url: '/companylist',
  icon: 'list'
}, {
  title: '我的浏览',
  url: '/mybrowse',
  icon: 'clipboard'
}, {
  title: '我的点赞',
  url: '/mylike',
  icon: 'thumbs-up'
}, {
  title: '我的收藏',
  url: '/mycollect',
  icon: 'heart'
}, {
  title: '给点意见',
  url: '/advice',
  icon: 'paper-plane'
}, {
  title: '检测更新',
  url: '/home',
  icon: 'swap'
}, {
  title: '关于我们',
  url: '/article',
  icon: 'logo-buffer'
}, {
  title: '帮助手册',
  url: '/article',
  icon: 'book'
},
];

constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar
) {
this.initializeApp();
}

initializeApp() {
this.platform.ready().then(() => {
  this.statusBar.styleDefault();
  this.splashScreen.hide();
});
}
ngOnInit() {
 this.user = ApiUrl.user;
 console.log(this.user);
}
}*/

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  // 用于判断返回键是否触发
  backButtonPressed: boolean;
  url: string;
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    // 导航控制器
    public navController: NavController,
    public toastCtrl: ToastController,
    private appMinimize: AppMinimize,
    private appUtils: AppUtils,
    private router: Router
  ) {
    this.backButtonPressed = false;
    this.initializeApp();
  }

  initializeApp() {
    this.initRouterListen();
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.registerBackButtonAction();     // 注册返回按键事件
      this.platform.resume.subscribe();    // 弹出框
    });
  }
  registerBackButtonAction() {
    this.platform.backButton.subscribe(() => {
      // if (this.url === '/tabs/(tab2:tab2)') {
      //   if (this.backButtonPressed) {
      //     // this.platform.exitApp();
      //     this.backButtonPressed = false;
      //     ApiUrl.txflag = false;
      //     this.appMinimize.minimize();
      //     // alert('退出应用');
      //   } else {
      //     // alert('再按一次退出应用');
      //     this.appUtils.presentToast('再按一次退出应用');
      //     this.backButtonPressed = true;
      //     setTimeout(() => this.backButtonPressed = false, 2000);
      //   }
      // } else {
        if (this.url.startsWith('/tabs') || this.url === '/') {
          if (this.backButtonPressed) {
            // this.platform.exitApp();
            this.backButtonPressed = false;
            ApiUrl.txflag = false;
            this.appMinimize.minimize();
            // alert('退出应用');
          } else {
            // alert('再按一次退出应用');
            this.appUtils.presentToast('再按一次退出应用');
            this.backButtonPressed = true;
            setTimeout(() => this.backButtonPressed = false, 2000);
          }
        } else {
          this.navController.goBack();
        }
      // }

    });
  }
  initRouterListen() {
    this.router.events.subscribe(event => { // 需要放到最后一个执行
      if (event instanceof NavigationEnd) {
        this.url = event.url;
      }
    });
  }
}
